package com.woniu.auth.controller;

import com.woniu.auth.entity.User;
import com.woniu.auth.service.UserService;
import com.woniu.auth.vo.LoginVo;
import com.woniu.commons.response.ResponseResult;
import com.woniu.commons.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/login")
    public ResponseResult<Object> login(@RequestBody LoginVo vo, HttpServletResponse response) {
        log.info("用户名：{}，密码：{}", vo.getUsername(), vo.getPassword());
        //调用service层进行登入
        User user = userService.login(vo);
        System.out.println("user:" + user);

        //假设登入成功：返回用户信息token
        String token = JWTUtil.generateToken(user.getUserId(), user.getUsername());

        //通过响应头将token返回给前端
        response.setHeader("authorization", token);

        return new ResponseResult<>(HttpStatus.SC_OK, "登入成功", user);
    }

    //通过token查询用户权限信息
    @GetMapping("/findPerms/{token}")
    public ResponseResult<List<String>> findPerms(@PathVariable("token") String token){
        List<String> list = userService.findPerms(token);
        return new ResponseResult<>(HttpStatus.SC_OK, "查询成功", list);
    }




}
